//*****************************************************************************
// 1. Års eksamen @ Datamatiker - EASJ
// Holmegaard Heldagsskole Administrations system 
// 
// Denne klasse er udarbejdet af: Marc og Kasper
//*****************************************************************************
package view.elev;

import control.ControlUtil;
import control.ElevHandler;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import model.*;
import view.Mainscreen;
import view.print.statusRapport;

public class OplysningerPanel extends javax.swing.JPanel {

    private ControlUtil controlUtil;
    private DefaultListModel fagModel;
    private DefaultListModel medicinModel;
    private DefaultListModel allergiModel;
    private DefaultListModel telefonModel;

    public OplysningerPanel(ControlUtil controlUtil) {

        this.controlUtil = controlUtil;
        fagModel = new DefaultListModel();
        medicinModel = new DefaultListModel();
        allergiModel = new DefaultListModel();
        telefonModel = new DefaultListModel();
        initComponents();
    }

    public void hentElever() {
        int value = vaelgElev.getSelectedIndex();
        vaelgElev.removeAllItems();
        vaelgElev.setEnabled(true);
        vaelgElev.addItem("Vælg elev");
        Elev[] el = controlUtil.getElevHandler().hentElever();
        for (int i = 0; i < el.length; i++) {
            vaelgElev.addItem(el[i]);
        }
        vaelgElev.setSelectedIndex(value);
    }

    private void opdaterElev(Elev elev) {

        fagModel.clear();
        HoldFag[] elevFag = controlUtil.getElevHandler().hentElevHoldFag(elev);
        for (int i = 0; i < elevFag.length; i++) {
            fagModel.addElement(elevFag[i]);
        }

        medicinModel.clear();
        ElevMedicin[] elevMedicin = controlUtil.getElevHandler().hentElevMedicin(elev);
        for (int i = 0; i < elevMedicin.length; i++) {
            medicinModel.addElement(elevMedicin[i]);
        }

        allergiModel.clear();
        ElevAllergi[] elevAllergi = controlUtil.getElevHandler().hentElevAllergi(elev);
        for (int i = 0; i < elevAllergi.length; i++) {
            allergiModel.addElement(elevAllergi[i]);
        }

        telefonModel.clear();
        ElevTelefon[] elTelefon = controlUtil.getElevHandler().hentElevTelefon(elev);
        for (int i = 0; i < elTelefon.length; i++) {
            telefonModel.addElement(elTelefon[i]);
        }

        note.setText(elev.getNote());
        CPR.setText(elev.getCpr());
        fornavn.setText(elev.getFornavn());
        efternavn.setText(elev.getEfternavn());
        hold.setText(elev.getHold().getHoldId());
        adresse.setText(elev.getAdresse());
        postnr.setText(elev.getPostnr().getPostNummer());
        by.setText(elev.getPostnr().getByen());
        rekvirerendeKomm.setText(elev.getElevRekvirskole().getPostnr().getByen());
        rekvirerendeSkole.setText(elev.getElevRekvirskole().getRekvirSkole());
        indskrevet.setText(elev.getIndskrivelse().toString());

        if (elev.getUdskrivning() != null) {
            udskrevet.setText(elev.getUdskrivning() + "");
        }

        if (elev.getBillede() != null) {
            Billede.setIcon(new ImageIcon(elev.getBillede()));
        }
    }

    private void rydFelter() {
        fagModel.clear();
        medicinModel.clear();
        allergiModel.clear();
        telefonModel.clear();
        indskrevet.setText("");
        udskrevet.setText("");
        note.setText("");
        rekvirerendeSkole.setText("");
        rekvirerendeKomm.setText("");
        CPR.setText("");
        fornavn.setText("");
        efternavn.setText("");
        hold.setText("");
        adresse.setText("");
        postnr.setText("");
        by.setText("");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        vaelgElev = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        CPR = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        fornavn = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        efternavn = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        adresse = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        postnr = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        by = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        Billede = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Allergier = new javax.swing.JList(allergiModel);
        jScrollPane2 = new javax.swing.JScrollPane();
        Medicin = new javax.swing.JList(medicinModel);
        jLabel13 = new javax.swing.JLabel();
        hold = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        fag = new javax.swing.JList(fagModel);
        jLabel20 = new javax.swing.JLabel();
        rekvirerendeKomm = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        rekvirerendeSkole = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        note = new javax.swing.JTextArea();
        jLabel16 = new javax.swing.JLabel();
        indskrevet = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        udskrevet = new javax.swing.JTextField();
        jScrollPane6 = new javax.swing.JScrollPane();
        telefonListe = new javax.swing.JList(telefonModel);
        udskrivKnap = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(1016, 600));
        setMinimumSize(new java.awt.Dimension(1016, 600));
        setPreferredSize(new java.awt.Dimension(1024, 600));
        setLayout(null);

        jLabel1.setText("Elev:    ");
        add(jLabel1);
        jLabel1.setBounds(20, 10, 60, 14);

        vaelgElev.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg Elev" }));
        vaelgElev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vaelgElevActionPerformed(evt);
            }
        });
        add(vaelgElev);
        vaelgElev.setBounds(20, 30, 190, 20);

        jLabel2.setText("CPR:");
        add(jLabel2);
        jLabel2.setBounds(20, 70, 80, 14);

        CPR.setEditable(false);
        add(CPR);
        CPR.setBounds(20, 90, 150, 20);

        jLabel3.setText("Fornavn:");
        add(jLabel3);
        jLabel3.setBounds(20, 120, 100, 14);

        fornavn.setEditable(false);
        add(fornavn);
        fornavn.setBounds(20, 140, 150, 20);

        jLabel4.setText("Efternavn:");
        add(jLabel4);
        jLabel4.setBounds(20, 170, 100, 14);

        efternavn.setEditable(false);
        add(efternavn);
        efternavn.setBounds(20, 190, 150, 20);

        jLabel5.setText("Adresse:");
        add(jLabel5);
        jLabel5.setBounds(20, 270, 100, 14);

        adresse.setEditable(false);
        add(adresse);
        adresse.setBounds(20, 290, 150, 20);

        jLabel6.setText("Post Nummer:");
        add(jLabel6);
        jLabel6.setBounds(20, 320, 130, 14);

        postnr.setEditable(false);
        add(postnr);
        postnr.setBounds(20, 340, 50, 20);

        jLabel7.setText("Telefon:");
        add(jLabel7);
        jLabel7.setBounds(20, 420, 90, 10);

        jLabel8.setText("By:");
        add(jLabel8);
        jLabel8.setBounds(20, 370, 70, 10);

        by.setEditable(false);
        add(by);
        by.setBounds(20, 390, 130, 20);

        jLabel9.setText("Billede:");
        add(jLabel9);
        jLabel9.setBounds(740, 30, 80, 14);

        jLabel10.setText("Allergier:");
        add(jLabel10);
        jLabel10.setBounds(620, 350, 90, 14);

        Billede.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        add(Billede);
        Billede.setBounds(740, 50, 250, 250);

        jLabel12.setText("Medicin:");
        add(jLabel12);
        jLabel12.setBounds(830, 350, 100, 14);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        Allergier.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        Allergier.setEnabled(false);
        Allergier.setFocusable(false);
        Allergier.setRequestFocusEnabled(false);
        jScrollPane1.setViewportView(Allergier);

        add(jScrollPane1);
        jScrollPane1.setBounds(620, 370, 160, 140);

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        Medicin.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        Medicin.setEnabled(false);
        Medicin.setFocusable(false);
        Medicin.setRequestFocusEnabled(false);
        jScrollPane2.setViewportView(Medicin);

        add(jScrollPane2);
        jScrollPane2.setBounds(830, 370, 160, 140);

        jLabel13.setText("Hold:");
        add(jLabel13);
        jLabel13.setBounds(20, 220, 90, 14);

        hold.setEditable(false);
        add(hold);
        hold.setBounds(20, 240, 90, 20);

        jLabel14.setText("Elevens Fag:");
        add(jLabel14);
        jLabel14.setBounds(490, 60, 110, 14);

        jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        fag.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        fag.setEnabled(false);
        fag.setFocusable(false);
        fag.setRequestFocusEnabled(false);
        jScrollPane3.setViewportView(fag);

        add(jScrollPane3);
        jScrollPane3.setBounds(490, 80, 160, 220);

        jLabel20.setText("Rekvirerende Kommune:");
        add(jLabel20);
        jLabel20.setBounds(260, 70, 170, 14);

        rekvirerendeKomm.setEditable(false);
        add(rekvirerendeKomm);
        rekvirerendeKomm.setBounds(260, 90, 130, 20);

        jLabel21.setText("Rekvirerende Skole:");
        add(jLabel21);
        jLabel21.setBounds(260, 120, 120, 14);

        rekvirerendeSkole.setEditable(false);
        add(rekvirerendeSkole);
        rekvirerendeSkole.setBounds(260, 140, 170, 20);

        jLabel15.setText("Kommentarer om eleven:");
        add(jLabel15);
        jLabel15.setBounds(260, 350, 320, 14);

        note.setColumns(20);
        note.setEditable(false);
        note.setRows(5);
        jScrollPane4.setViewportView(note);

        add(jScrollPane4);
        jScrollPane4.setBounds(260, 370, 320, 140);

        jLabel16.setText("Indskrevet:");
        add(jLabel16);
        jLabel16.setBounds(260, 200, 80, 14);

        indskrevet.setEditable(false);
        add(indskrevet);
        indskrevet.setBounds(260, 220, 110, 20);

        jLabel17.setText("Udskrevet:");
        add(jLabel17);
        jLabel17.setBounds(260, 250, 80, 14);

        udskrevet.setEditable(false);
        add(udskrevet);
        udskrevet.setBounds(260, 270, 110, 20);

        telefonListe.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        telefonListe.setEnabled(false);
        telefonListe.setFocusable(false);
        telefonListe.setRequestFocusEnabled(false);
        jScrollPane6.setViewportView(telefonListe);

        add(jScrollPane6);
        jScrollPane6.setBounds(20, 440, 100, 100);

        udskrivKnap.setText("Udskriv");
        udskrivKnap.setEnabled(false);
        udskrivKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                udskrivKnapActionPerformed(evt);
            }
        });
        add(udskrivKnap);
        udskrivKnap.setBounds(210, 540, 90, 23);
    }// </editor-fold>//GEN-END:initComponents

    private void vaelgElevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vaelgElevActionPerformed

        if (vaelgElev.getSelectedIndex() != 0 && vaelgElev.getItemCount() != 0) {
            opdaterElev((Elev) vaelgElev.getSelectedItem());
            udskrivKnap.setEnabled(true);
        } else {
            udskrivKnap.setEnabled(false);
            rydFelter();
        }
    }//GEN-LAST:event_vaelgElevActionPerformed

    private void udskrivKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_udskrivKnapActionPerformed
        if(vaelgElev.getSelectedIndex() != 0){
            Elev elev = (Elev) vaelgElev.getSelectedItem();
            statusRapport sr = new statusRapport(elev, controlUtil.getElevHandler().hentElevTelefon(elev),controlUtil.getElevHandler().hentKontaktPersonerElev(elev));
        sr.setVisible(true);        
        }
    }//GEN-LAST:event_udskrivKnapActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList Allergier;
    private javax.swing.JLabel Billede;
    private javax.swing.JTextField CPR;
    private javax.swing.JList Medicin;
    private javax.swing.JTextField adresse;
    private javax.swing.JTextField by;
    private javax.swing.JTextField efternavn;
    private javax.swing.JList fag;
    private javax.swing.JTextField fornavn;
    private javax.swing.JTextField hold;
    private javax.swing.JTextField indskrevet;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTextArea note;
    private javax.swing.JTextField postnr;
    private javax.swing.JTextField rekvirerendeKomm;
    private javax.swing.JTextField rekvirerendeSkole;
    private javax.swing.JList telefonListe;
    private javax.swing.JTextField udskrevet;
    private javax.swing.JButton udskrivKnap;
    private javax.swing.JComboBox vaelgElev;
    // End of variables declaration//GEN-END:variables
}
